{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eeaa2c41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 6.44 ms (started: 2021-08-13 01:29:26 +08:00)\n"
     ]
    }
   ],
   "source": [
    "# 自动计算cell的计算时间\n",
    "%load_ext autotime\n",
    "\n",
    "%config InlineBackend.figure_format='svg' #矢量图设置，让绘图更清晰"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0bd07701",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "origin\tgit@github.com:ustchope/kerastuner.git (fetch)\n",
      "origin\tgit@github.com:ustchope/kerastuner.git (push)\n",
      "[master 2a18a28] 更新 #1 Aug 13, 2021\n",
      " 1 file changed, 738 insertions(+), 11 deletions(-)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "To github.com:ustchope/kerastuner.git\n",
      "   d7e38b2..2a18a28  master -> master\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 4.02 s (started: 2021-08-13 01:59:36 +08:00)\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "\n",
    "# 增加更新\n",
    "git add *.ipynb\n",
    "\n",
    "git remote -v\n",
    "\n",
    "git commit -m '更新 #1 Aug 13, 2021'\n",
    "\n",
    "git push origin master"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6ee8129",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 1.42 s (started: 2021-08-13 01:29:26 +08:00)\n"
     ]
    }
   ],
   "source": [
    "#设置使用的gpu\n",
    "import tensorflow as tf\n",
    "\n",
    "gpus = tf.config.list_physical_devices(\"GPU\")\n",
    "\n",
    "if gpus:\n",
    "   \n",
    "    gpu0 = gpus[0] #如果有多个GPU，仅使用第0个GPU\n",
    "    tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用\n",
    "    # 或者也可以设置GPU显存为固定使用量(例如：4G)\n",
    "    #tf.config.experimental.set_virtual_device_configuration(gpu0,\n",
    "    #    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)]) \n",
    "    tf.config.set_visible_devices([gpu0],\"GPU\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50ac3d65",
   "metadata": {},
   "source": [
    "# 安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25b1870a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install keras-tuner -q"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdae96cb",
   "metadata": {},
   "source": [
    "# 介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f20160f4",
   "metadata": {},
   "source": [
    "以下是如何使用随机搜索为单层密集神经网络执行超参数调整。 首先，我们需要准备数据集——让我们以 MNIST 数据集为例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fc1b64a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 636 ms (started: 2021-08-13 01:29:36 +08:00)\n"
     ]
    }
   ],
   "source": [
    "from tensorflow import keras\n",
    "import numpy as np\n",
    "\n",
    "(x, y), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
    "\n",
    "x_train = x[:-10000]\n",
    "x_val = x[-10000:]\n",
    "y_train = y[:-10000]\n",
    "y_val = y[-10000:]\n",
    "\n",
    "x_train = np.expand_dims(x_train, -1).astype(\"float32\") / 255.0\n",
    "x_val = np.expand_dims(x_val, -1).astype(\"float32\") / 255.0\n",
    "x_test = np.expand_dims(x_test, -1).astype(\"float32\") / 255.0\n",
    "\n",
    "num_classes = 10\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_val = keras.utils.to_categorical(y_val, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73f41622",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 28, 28, 1)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 14 ms (started: 2021-08-13 01:10:08 +08:00)\n"
     ]
    }
   ],
   "source": [
    "x_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae0d702d",
   "metadata": {},
   "source": [
    "# 准备模型构建函数\n",
    "\n",
    "然后，我们定义一个模型构建函数。 它接受一个参数 hp，您可以从中采样超参数，例如`hp.Int('units', min_value=32, max_value=512, step=32)` （某个范围内的整数）。\n",
    "\n",
    "此函数返回一个编译模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "857d523a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 386 ms (started: 2021-08-13 01:29:39 +08:00)\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras import layers\n",
    "from keras_tuner import RandomSearch\n",
    "\n",
    "\n",
    "def build_model(hp):\n",
    "    model = keras.Sequential()\n",
    "    model.add(layers.Flatten())\n",
    "    model.add(\n",
    "        layers.Dense(\n",
    "            units=hp.Int(\"units\", min_value=32, max_value=512, step=32),\n",
    "            activation=\"relu\",\n",
    "        )\n",
    "    )\n",
    "    model.add(layers.Dense(10, activation=\"softmax\"))\n",
    "    model.compile(\n",
    "        optimizer=keras.optimizers.Adam(\n",
    "            hp.Choice(\"learning_rate\", values=[1e-2, 1e-3, 1e-4])\n",
    "        ),\n",
    "        loss=\"categorical_crossentropy\",\n",
    "        metrics=[\"accuracy\"],\n",
    "    )\n",
    "    return model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c67794f",
   "metadata": {},
   "source": [
    "# 开始搜索\n",
    "接下来，让我们实例化一个调谐器。 您应该指定模型构建函数、要优化的目标的名称（对于内置指标自动推断是最小化还是最大化）、要测试的试验总数 (`max_trials`) 以及应该使用的模型数 构建并适合每次试验 (`executions_per_trial`)。\n",
    "\n",
    "我们使用 `overwrite` 参数来控制是覆盖同一目录中的先前结果还是恢复先前的搜索。 在这里，我们设置 `overwrite=True` 以开始新的搜索并忽略任何先前的结果。\n",
    "\n",
    "可用的调谐器有 `RandomSearch`、`BayesianOptimization` 和 `Hyperband`。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5e79a1b",
   "metadata": {},
   "source": [
    "> **注意**：每次试验多次执行的目的是减少结果差异，从而能够更准确地评估模型的性能。 如果您想更快地获得结果，您可以设置 executions_per_trial=1（每个模型配置的单轮训练）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cda9696d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 1.42 s (started: 2021-08-13 01:29:41 +08:00)\n"
     ]
    }
   ],
   "source": [
    "tuner = RandomSearch(\n",
    "    build_model,\n",
    "    objective=\"val_accuracy\",\n",
    "    max_trials=3,\n",
    "    executions_per_trial=2,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdc72cb3",
   "metadata": {},
   "source": [
    "您可以打印搜索空间的摘要："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "016e2f0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Search space summary\n",
      "Default search space size: 2\n",
      "units (Int)\n",
      "{'default': None, 'conditions': [], 'min_value': 32, 'max_value': 512, 'step': 32, 'sampling': None}\n",
      "learning_rate (Choice)\n",
      "{'default': 0.01, 'conditions': [], 'values': [0.01, 0.001, 0.0001], 'ordered': True}\n",
      "time: 1.43 ms (started: 2021-08-13 01:29:45 +08:00)\n"
     ]
    }
   ],
   "source": [
    "tuner.search_space_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc76214",
   "metadata": {},
   "source": [
    "然后，开始搜索最佳超参数配置。 对搜索的调用与 model.fit() 具有相同的签名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b964d48e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 01m 40s]\n",
      "val_accuracy: 0.9417499899864197\n",
      "\n",
      "Best val_accuracy So Far: 0.9543499946594238\n",
      "Total elapsed time: 00h 05m 01s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 5min 1s (started: 2021-08-13 01:30:10 +08:00)\n"
     ]
    }
   ],
   "source": [
    "tuner.search(x_train, y_train, epochs=2, validation_data=(x_val, y_val))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb52ac6e",
   "metadata": {},
   "source": [
    "以下是搜索中发生的事情：通过调用模型构建函数迭代地构建模型，该函数填充由 hp 对象跟踪的超参数空间（搜索空间）。 调谐器逐步探索空间，记录每个配置的指标。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9178469b",
   "metadata": {},
   "source": [
    "# 查询结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e67b0b55",
   "metadata": {},
   "source": [
    "搜索结束后，您可以检索最佳模型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8cd3d08e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 297 ms (started: 2021-08-13 01:37:44 +08:00)\n"
     ]
    }
   ],
   "source": [
    "models = tuner.get_best_models(num_models=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f21c5bc4",
   "metadata": {},
   "source": [
    "或打印结果摘要："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b35d7257",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Results summary\n",
      "Results in my_dir/helloworld\n",
      "Showing 10 best trials\n",
      "Objective(name='val_accuracy', direction='max')\n",
      "Trial summary\n",
      "Hyperparameters:\n",
      "units: 320\n",
      "learning_rate: 0.01\n",
      "Score: 0.9543499946594238\n",
      "Trial summary\n",
      "Hyperparameters:\n",
      "units: 32\n",
      "learning_rate: 0.01\n",
      "Score: 0.9505999982357025\n",
      "Trial summary\n",
      "Hyperparameters:\n",
      "units: 288\n",
      "learning_rate: 0.0001\n",
      "Score: 0.9417499899864197\n",
      "time: 2.43 ms (started: 2021-08-13 01:38:05 +08:00)\n"
     ]
    }
   ],
   "source": [
    "tuner.results_summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f31c06d",
   "metadata": {},
   "source": [
    "您还可以在文件夹 my_dir/helloworld 中找到详细的日志、检查点等，即目录/项目名称。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26120ab5",
   "metadata": {},
   "source": [
    "# 搜索空间可能包含条件超参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "325ece45",
   "metadata": {},
   "source": [
    "下面，我们有一个 for 循环，用于创建可调数量的层，这些层本身涉及一个可调单位参数。\n",
    "\n",
    "这可以推到任何级别的参数相互依赖性，包括递归。\n",
    "\n",
    "请注意，所有参数名称都应该是唯一的（这里，在 i 上的循环中，我们将内部参数命名为“units_”+ str(i)）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d7369306",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 1.58 ms (started: 2021-08-13 01:44:18 +08:00)\n"
     ]
    }
   ],
   "source": [
    "def build_model(hp):\n",
    "    model = keras.Sequential()\n",
    "    model.add(layers.Flatten())\n",
    "    for i in range(hp.Int(\"num_layers\", 2, 20)):\n",
    "        model.add(\n",
    "            layers.Dense(\n",
    "                units=hp.Int(\"units_\" + str(i), min_value=32, max_value=512, step=32),\n",
    "                activation=\"relu\",\n",
    "            )\n",
    "        )\n",
    "    model.add(layers.Dense(10, activation=\"softmax\"))\n",
    "    model.compile(\n",
    "        optimizer=keras.optimizers.Adam(hp.Choice(\"learning_rate\", [1e-2, 1e-3, 1e-4])),\n",
    "        loss=\"categorical_crossentropy\",\n",
    "        metrics=[\"accuracy\"],\n",
    "    )\n",
    "    return model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19549017",
   "metadata": {},
   "source": [
    "# 使用 HyperModel 子类而不是模型构建函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bc64db0",
   "metadata": {},
   "source": [
    "这使得共享和重用超模型变得容易。\n",
    "\n",
    "HyperModel 子类只需要实现一个 build(self, hp) 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5c1eb651",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 2.36 ms (started: 2021-08-13 01:47:22 +08:00)\n"
     ]
    }
   ],
   "source": [
    "from keras_tuner import HyperModel\n",
    "\n",
    "\n",
    "class MyHyperModel(HyperModel):\n",
    "    def __init__(self, classes):\n",
    "        self.classes = classes\n",
    "\n",
    "    def build(self, hp):\n",
    "        model = keras.Sequential()\n",
    "        model.add(layers.Flatten())\n",
    "        model.add(\n",
    "            layers.Dense(\n",
    "                units=hp.Int(\"units\", min_value=32, max_value=512, step=32),\n",
    "                activation=\"relu\",\n",
    "            )\n",
    "        )\n",
    "        model.add(layers.Dense(self.classes, activation=\"softmax\"))\n",
    "        model.compile(\n",
    "            optimizer=keras.optimizers.Adam(\n",
    "                hp.Choice(\"learning_rate\", values=[1e-2, 1e-3, 1e-4])\n",
    "            ),\n",
    "            loss=\"categorical_crossentropy\",\n",
    "            metrics=[\"accuracy\"],\n",
    "        )\n",
    "        return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "28c76ed4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 00m 49s]\n",
      "val_accuracy: 0.9469000101089478\n",
      "\n",
      "Best val_accuracy So Far: 0.975600004196167\n",
      "Total elapsed time: 00h 02m 29s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 2min 30s (started: 2021-08-13 01:48:00 +08:00)\n"
     ]
    }
   ],
   "source": [
    "hypermodel = MyHyperModel(classes=10)\n",
    "\n",
    "tuner = RandomSearch(\n",
    "    hypermodel,\n",
    "    objective=\"val_accuracy\",\n",
    "    max_trials=3,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")\n",
    "\n",
    "tuner.search(x_train, y_train, epochs=2, validation_data=(x_val, y_val))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d914a8b5",
   "metadata": {},
   "source": [
    "# 预制的可调应用程序：HyperResNet 和 HyperXception"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6935b14a",
   "metadata": {},
   "source": [
    "这些是用于计算机视觉的即用型超模型。\n",
    "\n",
    "它们使用 loss=\"categorical_crossentropy\" 和 metrics=[\"accuracy\"] 进行预编译。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6669c557",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 00m 14s]\n",
      "val_accuracy: 0.10000000149011612\n",
      "\n",
      "Best val_accuracy So Far: 0.10999999940395355\n",
      "Total elapsed time: 00h 01m 45s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 1min 46s (started: 2021-08-13 01:51:39 +08:00)\n"
     ]
    }
   ],
   "source": [
    "from keras_tuner.applications import HyperResNet\n",
    "\n",
    "hypermodel = HyperResNet(input_shape=(28, 28, 1), classes=10)\n",
    "\n",
    "tuner = RandomSearch(\n",
    "    hypermodel,\n",
    "    objective=\"val_accuracy\",\n",
    "    max_trials=3,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")\n",
    "\n",
    "tuner.search(\n",
    "    x_train[:100], y_train[:100], epochs=1, validation_data=(x_val[:100], y_val[:100])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3ab03f1",
   "metadata": {},
   "source": [
    "# 可以轻松地将搜索空间限制为几个参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea5620e6",
   "metadata": {},
   "source": [
    "如果您有一个现有的超模型，并且只想搜索几个参数（例如学习率），则可以通过将超参数参数传递给调谐器构造函数以及 tune_new_entries=False 来指定那些参数 你没有在超参数中列出不应该被调整。 对于这些参数，使用默认值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "995a9960",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 00m 02s]\n",
      "val_accuracy: 0.09000000357627869\n",
      "\n",
      "Best val_accuracy So Far: 0.11999999731779099\n",
      "Total elapsed time: 00h 00m 09s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 10.7 s (started: 2021-08-13 01:54:58 +08:00)\n"
     ]
    }
   ],
   "source": [
    "from keras_tuner import HyperParameters\n",
    "from keras_tuner.applications import HyperXception\n",
    "\n",
    "hypermodel = HyperXception(input_shape=(28, 28, 1), classes=10)\n",
    "\n",
    "hp = HyperParameters()\n",
    "\n",
    "# This will override the `learning_rate` parameter with your\n",
    "# own selection of choices\n",
    "hp.Choice(\"learning_rate\", values=[1e-2, 1e-3, 1e-4])\n",
    "\n",
    "tuner = RandomSearch(\n",
    "    hypermodel,\n",
    "    hyperparameters=hp,\n",
    "    # `tune_new_entries=False` prevents unlisted parameters from being tuned\n",
    "    tune_new_entries=False,\n",
    "    objective=\"val_accuracy\",\n",
    "    max_trials=3,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")\n",
    "\n",
    "tuner.search(\n",
    "    x_train[:100], y_train[:100], epochs=1, validation_data=(x_val[:100], y_val[:100])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15272d8e",
   "metadata": {},
   "source": [
    "# 关于参数默认值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ffd1d62",
   "metadata": {},
   "source": [
    "每当您在模型构建函数或超模型的构建方法中注册超参数时，您都可以指定默认值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "255f349f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "128"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time: 3.94 ms (started: 2021-08-13 01:56:04 +08:00)\n"
     ]
    }
   ],
   "source": [
    "hp.Int(\"units\", min_value=32, max_value=512, step=32, default=128)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0369086b",
   "metadata": {},
   "source": [
    "如果你不这样做，超参数总是有一个默认的默认值（对于 Int，它等于 min_value）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b428356",
   "metadata": {},
   "source": [
    "# 固定超模型中的值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91bf1e54",
   "metadata": {},
   "source": [
    "如果你想做相反的事情——调整超模型中的所有可用参数，除了一个（学习率）之外怎么办？\n",
    "\n",
    "使用 Fixed 条目（或任意数量的 Fixed 条目）传递超参数参数，并指定 tune_new_entries=True。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c99bd489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 00m 04s]\n",
      "val_accuracy: 0.12999999523162842\n",
      "\n",
      "Best val_accuracy So Far: 0.12999999523162842\n",
      "Total elapsed time: 00h 00m 13s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 13.5 s (started: 2021-08-13 01:57:33 +08:00)\n"
     ]
    }
   ],
   "source": [
    "hypermodel = HyperXception(input_shape=(28, 28, 1), classes=10)\n",
    "\n",
    "hp = HyperParameters()\n",
    "hp.Fixed(\"learning_rate\", value=1e-4)\n",
    "\n",
    "tuner = RandomSearch(\n",
    "    hypermodel,\n",
    "    hyperparameters=hp,\n",
    "    tune_new_entries=True,\n",
    "    objective=\"val_accuracy\",\n",
    "    max_trials=3,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")\n",
    "\n",
    "tuner.search(\n",
    "    x_train[:100], y_train[:100], epochs=1, validation_data=(x_val[:100], y_val[:100])\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83d50d2d",
   "metadata": {},
   "source": [
    "# 覆盖编译参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19f71d94",
   "metadata": {},
   "source": [
    "如果您有一个想要更改现有优化器、损失或指标的超模型，您可以通过将这些参数传递给调谐器构造函数来实现："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "752b4584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Trial 3 Complete [00h 00m 06s]\n",
      "val_loss: 0.08998560160398483\n",
      "\n",
      "Best val_loss So Far: 0.08998209983110428\n",
      "Total elapsed time: 00h 00m 13s\n",
      "INFO:tensorflow:Oracle triggered exit\n",
      "time: 13.7 s (started: 2021-08-13 01:58:46 +08:00)\n"
     ]
    }
   ],
   "source": [
    "hypermodel = HyperXception(input_shape=(28, 28, 1), classes=10)\n",
    "\n",
    "tuner = RandomSearch(\n",
    "    hypermodel,\n",
    "    optimizer=keras.optimizers.Adam(1e-3),\n",
    "    loss=\"mse\",\n",
    "    metrics=[\n",
    "        keras.metrics.Precision(name=\"precision\"),\n",
    "        keras.metrics.Recall(name=\"recall\"),\n",
    "    ],\n",
    "    objective=\"val_loss\",\n",
    "    max_trials=3,\n",
    "    overwrite=True,\n",
    "    directory=\"my_dir\",\n",
    "    project_name=\"helloworld\",\n",
    ")\n",
    "\n",
    "tuner.search(\n",
    "    x_train[:100], y_train[:100], epochs=1, validation_data=(x_val[:100], y_val[:100])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dd36819",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2keras]",
   "language": "python",
   "name": "conda-env-tf2keras-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
